# os 函数

import os

# 当前目录
# print(os.getcwd())

# 文件拷贝
# import shutil
# import os
#
# if not os.path.exists("D:\\z"):
#     os.mkdir("D:\\z")
# else:
#     print("文件夹存在")
# try:
#     shutil.copyfile("D:\\a.txt", "D:\\z\\az.txt")
#     print("拷贝成功")
# except OSError as err:
#     print("拷贝异常了", err)

# 重命名
# import shutil
#
# shutil.move("D:\\z\\az.txt", "D:\\z\\zz.txt")

# 扫描文件
# import glob
#
# glob_glob = glob.glob("D:\\z\\*.txt")
# print(glob_glob)

# 执行cmd命令
# import os
#
# os.system("calc")


# import subprocess
#
# # 调用计算器
# subprocess.Popen("calc")


# 访问url
# from urllib.request import urlopen
#
# line = urlopen("https://www.baidu.com")
# for l in line:
#     print(l)

# 发送邮件
# import smtplib
# from email.mime.text import MIMEText
# from email.mime.multipart import MIMEMultipart
#
# # 邮件内容
# subject = "学习python"
# body = "这是一封学习py发送的测试邮件。"
# sender_email = "775610843@qq.com"
# receiver_email = "775610843@qq.com"
#
# # 创建邮件
# msg = MIMEMultipart()
# msg["From"] = sender_email
# msg["To"] = receiver_email
# msg["Subject"] = subject
# msg.attach(MIMEText(body, "plain"))
#
# # SMTP服务器设置
# smtp_server = "smtp.qq.com"
# smtp_username = "775610843@qq.com"
# smtp_password = "ouixviivtbdubedc"
#
# # 发送邮件
# try:
#     with smtplib.SMTP(smtp_server) as server:
#         server.starttls()
#         server.login(smtp_username, smtp_password)
#         server.sendmail(sender_email, receiver_email, msg.as_string())
#     print("邮件发送成功！")
# except Exception as e:
#     print(f"邮件发送失败：{str(e)}")

# 时间日期
# from datetime import date
#
# today = date.today()
# print(today)
#
# birthDay = date(1996, 10, 13)
# a = today - birthDay
# print(a)

# 压缩解压

# import zlib
#
# s = b"liqjdlkashjdcnkjsdhaksdhadkjals;kdjqaslkndcaochjkaslkdjalkdjaslkdj"
# print(len(s))
#
# compress = zlib.compress(s)
# print(len(compress))
#
# decompress = zlib.decompress(compress)
# print(decompress)

# 计算笛卡尔积
# import itertools
#
#
# class Dke:
#     def __init__(self):
#         self.dataList = []
#
#     def addDataList(self, dl):
#         self.dataList.append(dl)
#
#     def buildDke(self):
#         for res in itertools.product(*self.dataList):
#             # * 是解包操作符，*self.dataList会将这个列表拆分为3个独立的参数，
#             # 即[1, 2, 3]、["a", "b", "c"]和["q", "w"]。然后，itertools.product()函数会将这些参数作为输入，生成它们的笛卡尔积
#             print(res)
#
#
# dke = Dke()
# dke.addDataList([1, 2, 3])
# dke.addDataList(["a", "b", "c"])
# dke.addDataList(["q", "w"])
# dke.buildDke()


# 数据库操作
# from pymysql import Connect
#
# host = 'cxs-vm'
# user = 'root'
# password = '123456'
# port = 3307  # 指定端口号
#
# # 连接MySQL数据库
# db = Connect(host=host, user=user, password=password, port=port, autocommit=True)
#
# cursor = db.cursor()
#
# db.select_db("test")

# cursor.execute("insert into py_study(`name`,`age`) values('cxs',18) ")
#
# # 上面加了autocommit 这里不需要了
# # db.commit()
# cursor.close()

# cursor.execute("select * from py_study")
#
# res = cursor.fetchall()
# for r in res:
#     print(r)
#
# db.close()


# 线程 + 锁


# import threading
# import time
#
# exitFlag = 0
#
#
# # 集成Tread类
# class myThread(threading.Thread):
#     def __init__(self, threadID, name, counter):
#         threading.Thread.__init__(self)
#         self.threadID = threadID
#         self.name = name
#         self.counter = counter
#
#     def run(self):
#         print("开始线程：" + self.name)
#         print_time(self.name, self.counter, 5)
#         print("退出线程：" + self.name)
#
#
# def print_time(threadName, delay, counter):
#     while counter:
#         if exitFlag:
#             threadName.exit()
#         time.sleep(delay)
#         print("%s: %s" % (threadName, time.ctime(time.time())))
#         counter -= 1
#
#
# # 创建新线程
# thread1 = myThread(1, "Thread-1", 1)
# thread2 = myThread(2, "Thread-2", 2)
#
# # 开启新线程
# thread1.start()
# thread2.start()
# thread1.join()
# thread2.join()
# print("退出主线程")


import threading


class Printer:
    def __init__(self):
        self.condition = threading.Condition()
        self.turn = 1

    def print_a(self):
        for _ in range(5):
            with self.condition:
                while self.turn != 1:
                    self.condition.wait()
                print("A")
                self.turn = 2
                self.condition.notify_all()

    def print_b(self):
        for _ in range(5):
            with self.condition:
                while self.turn != 2:
                    self.condition.wait()
                print("B")
                self.turn = 1
                self.condition.notify_all()


# 创建Printer对象
printer = Printer()

# 创建两个线程，并启动
thread_a = threading.Thread(target=printer.print_a)
thread_b = threading.Thread(target=printer.print_b)
thread_a.start()
thread_b.start()

# 等待两个线程执行完毕
thread_a.join()
thread_b.join()
